// test/test_logger.cpp
#include "logger.h"
#include <iostream>
#include <fstream>
#include <string>
#include <thread>
#include <vector>
#include <chrono>
#include <cstdio>  // 替代filesystem，用于文件操作

using namespace util;
using namespace std;

// 检查文件是否存在（兼容旧编译器）
bool fileExists(const string& path) {
    FILE* file = fopen(path.c_str(), "r");
    if (file) {
        fclose(file);
        return true;
    }
    return false;
}

// 基础功能测试
void testBasicFunction() {
    cout << "Testing basic functionality..." << endl;
    const string logFile = "test_basic.log";
    
    Logger::instance().init(logFile, 1024*1024, 1);
    Logger::instance().setLevel(LogLevel::INFO);
    
    LOG_INFO("Basic test: info message");
    LOG_WARN("Basic test: warning message");
    LOG_ERROR("Basic test: error message");
    
    // std::this_thread::sleep_for(std::chrono::seconds(2)); 

    // Logger::instance().flush();
    
    if (fileExists(logFile)) {
        cout << "Basic test passed: log file created" << endl;
    } else {
        cerr << "Basic test failed: log file not found" << endl;
    }
}

// 多线程测试
void testMultiThread() {
    cout << "\nTesting multi-threaded logging..." << endl;
    const string logFile = "test_multithread.log";
    
    Logger::instance().init(logFile, 1024*1024, 1);
    Logger::instance().setLevel(LogLevel::INFO);
    
    const int numThreads = 5;
    const int numLogs = 1000;
    
    auto worker = [numLogs](int id) {
        for (int i = 0; i < numLogs; ++i) {
            LOG_INFO("Thread %d: message %d", id, i);
        }
    };
    
    vector<thread> threads;
    for (int i = 0; i < numThreads; ++i) {
        threads.emplace_back(worker, i);
    }
    
    for (auto& t : threads) {
        t.join();
    }
    
    // Logger::instance().flush();
    
    if (fileExists(logFile)) {
        cout << "Multi-thread test passed: log file created" << endl;
    } else {
        cerr << "Multi-thread test failed: log file not found" << endl;
    }
}

// 日志滚动测试（简化版，不依赖filesystem）
void testLogRolling() {
    cout << "\nTesting log rolling..." << endl;
    const string baseName = "test_rolling";
    
    Logger::instance().init(baseName, 1024, 1);  // 小滚动尺寸
    Logger::instance().setLevel(LogLevel::INFO);
    
    // 写入大量日志触发滚动
    for (int i = 0; i < 1000; ++i) {
        LOG_INFO("Large message to trigger rolling: %s", string(500, 'x').c_str());
    }
    
    // Logger::instance().flush();
    
    // 检查是否生成多个文件（通过简单文件名匹配）
    int count = 0;
    // 尝试打开可能的滚动文件（假设格式为baseName.时间戳.pid.log）
    for (int i = 0; i < 5; ++i) {  // 检查前5个可能的文件
        char fileName[256];
        snprintf(fileName, sizeof(fileName), "%s.%d.log", baseName.c_str(), i);
        if (fileExists(fileName)) {
            count++;
        }
    }
    
    if (count >= 2) {
        cout << "Log rolling test passed: " << count << " files created" << endl;
    } else {
        cerr << "Log rolling test failed: only " << count << " file created" << endl;
    }
}

int main() {
    testBasicFunction();
    testMultiThread();
    testLogRolling();
    
    Logger::instance().shutdown();
    cout << "\nAll tests completed" << endl;
    return 0;
}